package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entry.SysUser;
import com.itheima.ydd.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 *后台系统用户表 sys_user
 */
@RequestMapping("/index")
@RestController
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    public R login(@RequestBody SysUser user, HttpSession session){
        log.info("登录用户名:"+user.getLogin_name());
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getLogin_name,user.getLogin_name());
        lqw.eq(SysUser::getPassword,password);
        SysUser sysUser = sysUserService.getOne(lqw);
        if(sysUser != null){
            session.setAttribute("sysUser",sysUser.getId());
            return R.success(sysUser);
        }
        return R.error("用户名或密码有误!");
    }
}
